<?php
namespace Common\Controller;
use Think\Controller;

class BaseController extends Controller {

	public $config,$pagesize,$user,$admin;

	protected function _initialize(){
		//初始化系统配置信息
		$this->setConfig();
		$this->getFoodCate();
		$this->pagesize = C('PAGESIZE') ? C('PAGESIZE') : '6';
		if (strtolower(MODULE_NAME) == 'admin') {
			$_initMethod = '_admin';
		}elseif(strtolower(MODULE_NAME) == 'user' && strtolower(CONTROLLER_NAME) != 'admin'){
			$_initMethod = '_member';
		}else{
			$_initMethod = '_' . strtolower(CONTROLLER_NAME);
		}

		if(method_exists($this , $_initMethod )) {
			$this->$_initMethod();
		} else {
			$this->_index();
		}
		$this->assign('user',session('user'));   //登入信息赋值
		$this->user = session('user');
		$this->admin = session('admin');
	}

	public function _index(){
	}

	public function _member(){
		if (!is_login() && strtolower(CONTROLLER_NAME) != 'public') {
			redirect(U('User/Public/login'));
		}else{
			$this->getUserMenu();
		}
	}

	public function _admin(){
		if (!is_administrator() && strtolower(CONTROLLER_NAME) != 'public') {
			$this->redirect('Admin/Public/login');
		}else{
			$this->assign('admin',session('admin'));   //登入信息赋值
		}
	}

	public function setConfig(){
		//系统配置信息
		$this->config = F('system_config');
		if (!$this->config) {
			//从数据库获取，然后进行缓存
			$data = D('Config')->select();
			foreach ($data as $key => $value) {
				$config[$value['name']] = $value['value'];
			}
			$this->config = $config;
			F('system_config',$this->config);
		}
		$this->assign('conf',$this->config);
	}

	//用于Widget获取模板地址并输出
	public function template($template){
		$this->display(T($template));
	}

	public function writelog($filename,$content){
		if ($filename) {
			$fp=fopen($filename,'a');
			fwrite($fp,$content);
			fclose($fp);
		}
	}

	public function getUserMenu(){
		
	}

	public function getFoodCate(){
		$Foodcat=M('Foodcat');
		$foodcatlist=$Foodcat->select();
		$this->assign('food_cate',$foodcatlist);
	}

}